package com.ananjt.system.vo;

import com.ananjt.system.entity.SpMenu;
import com.ananjt.system.entity.SpUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Data
public class SecurityUserDetails extends SpUser implements UserDetails {
    private static final long serialVersionUID = 1L;

    private List<SpMenu> menus;

    public SecurityUserDetails() {

    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        // 设置通用默认权限
        authorityList.add(new SimpleGrantedAuthority("ROLE_CAH"));

        List<SpMenu> menus = this.getMenus();
        if (menus != null && menus.size() > 0) {
            for (SpMenu menu : menus) {
                if (!StringUtils.isEmpty(menu.getPermission())) {
                    authorityList.add(new SimpleGrantedAuthority(menu.getPermission()));
                }
            }
        }
        return authorityList;
    }

    @Override
    public String getUsername() {
        return this.getUserName();
    }

    /**
     * 账户是否过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否禁用
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
